package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwapT;
import cn.e4j.modular.crm.mapper.GwapTMapper;
import cn.e4j.modular.crm.service.GwapTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-01-16
 */
@Service
public class GwapTServiceImpl extends ServiceImpl<GwapTMapper, GwapT> implements GwapTService {
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwapDtos = this.baseMapper.listPage(page, dto);

        return gwapDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwamList(Dto dto) {

        List<Map<String, Object>> gwamDtos = this.baseMapper.getGwamList(dto);

        return gwamDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGweaList(Dto dto) {

        List<Map<String, Object>> gweaDtos = this.baseMapper.getGweaList(dto);

        return gweaDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwafList(Dto dto) {

        List<Map<String, Object>> gwafDtos = this.baseMapper.getGwafList(dto);

        return gwafDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwacList(Dto dto) {

        List<Map<String, Object>> gwacDtos = this.baseMapper.getGwacList(dto);

        return gwacDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwabList(Dto dto) {

        List<Map<String, Object>> gwabDtos = this.baseMapper.getGwabList(dto);

        return gwabDtos;
    }

    @Transactional(readOnly = true)
    public GwapT getByKey(Dto dto) {
        GwapT gwap = this.baseMapper.getByKey(dto);
        return gwap;
    }

    @Transactional
    @Override
    public void add(GwapT gwapT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwapT.setGWAP002(" ");
        gwapT.setGWAP003(" ");
        gwapT.setGWAP009(" ");
        gwapT.setGWAP010(" ");
        gwapT.setGWAP011(" ");
        gwapT.setGWAP012(" ");
        gwapT.setGWAP013(" ");
        gwapT.setGWAPENT(shiroUser.getDBC());
        gwapT.setGWAPUSER(shiroUser.getACCOUNT());
        gwapT.setGWAPGRUP(shiroUser.getORG_ID());
        gwapT.setGWAPDATE(LocalDate.now());
        gwapT.setGWAPTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwapT.setGWAPORIG(shiroUser.getORG_ID());
        gwapT.setGWAPORIU(shiroUser.getACCOUNT());
        gwapT.insert();
    }

    @Transactional
    @Override
    public void edit(GwapT gwap) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwap.setGWAPMODU(shiroUser.getACCOUNT());
        UpdateWrapper<GwapT> gwapTUpdateWrapper = new UpdateWrapper<>();
        gwapTUpdateWrapper.eq("GWAPENT", gwap.getGWAPENT());
        gwapTUpdateWrapper.eq("GWAP001", gwap.getGWAP001());
        gwapTUpdateWrapper.eq("GWAP002", gwap.getGWAP002());
        gwap.update(gwapTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delete(GwapT gwapT) {
        UpdateWrapper<GwapT> gwapTUpdateWrapper = new UpdateWrapper<>();
        gwapTUpdateWrapper.eq("GWAPENT", gwapT.getGWAPENT());
        gwapTUpdateWrapper.eq("GWAP001", gwapT.getGWAP001());
        gwapTUpdateWrapper.eq("GWAP002", gwapT.getGWAP002());
        gwapT.delete(gwapTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwapT> gwapTList) {
        for (GwapT gwapT : gwapTList) {
            UpdateWrapper<GwapT> gwapTUpdateWrapper = new UpdateWrapper<>();
            gwapTUpdateWrapper.eq("GWAPENT", gwapT.getGWAPENT());
            gwapTUpdateWrapper.eq("GWAP001", gwapT.getGWAP001());
            gwapTUpdateWrapper.eq("GWAP002", gwapT.getGWAP002());
            gwapT.delete(gwapTUpdateWrapper);
        }
    }
}
